﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using WorkFlowSystem.DataBase;
using System.Data.SqlClient;
using DevComponents.DotNetBar.Controls;
using System.Collections;
using WorkFlowSystem.Common;

namespace WorkFlowSystem.Business
{
    public partial class WorkInfoInput : Form
    {
        public WorkInfoInput(bool isBossFlag, bool isPreViewFlag, string uid, string sid, string w_start_date, string w_end_date,string w_status)
        {
            this.uid = uid;
            this.sid = sid;
            this.w_start_date = w_start_date;
            this.w_end_date = w_end_date;
            this.w_status = w_status;
            this.isBossFlag = isBossFlag;
            this.isPreViewFlag = isPreViewFlag;

            InitializeComponent();

            if (0 == this.showStandard())
            {
                dataExistFlag = false;
            }
            else
            {
                dataExistFlag = true;
            }
                        
        }

        private bool isBossFlag;
        private bool isPreViewFlag;
        private string w_status;
        private string uid;
        private string sid;
        private string w_start_date;
        private string w_end_date;
        private RadioButton[] rba = new RadioButton[5];
        private bool inputCheck = true;


        public bool dataExistFlag = false;

        //记录填写详细数据表示
        public int showStandard()
        {
            DBOperator dbo = new DBOperator();

            DataTable dt = new DataTable();

            dbo.ConnectDB();

            //获取标准信息集
            string sql = " select s.*,w.w_date_name,w.w_tab_1,w.w_tab_2,w.w_tab_3,w.w_tab_4,w.w_tab_5,w.w_tab_6,w.w_tab_7,w.w_tab_8,w.w_tab_9,w.w_tab_10, w.w_notes, w.w_manager_notes, CONVERT(varchar(10), w.w_start_date, 120) as w_start_date " +
            " from standard s,work_info w " +
            " where sid = @sid and w.w_sid = s.sid and w.w_start_date = @w_start_date  and w.w_end_date = @w_end_date and w_uid = @uid " +
            " order by s_tab_no desc ";
            
            List<SqlParameter> para_list = new List<SqlParameter>();

            para_list.Add(new SqlParameter("@sid", this.sid));
            para_list.Add(new SqlParameter("@uid", this.uid));
            para_list.Add(new SqlParameter("@w_start_date", this.w_start_date));
            para_list.Add(new SqlParameter("@w_end_date", this.w_end_date));
            

            dbo.ExecuteDataTable(sql, para_list, false, ref dt);

            dbo.CloseDB();

            int dataCnt = 0;

            dataCnt = dt.Rows.Count;

            if (dataCnt == 0)
            {
                MessageBox.Show("无该记录对应标准。");
                return 0;
            }
            else
            {
                if (isPreViewFlag)
                {
                    buttonX1.Enabled = false;
                    buttonX2.Enabled = false;
                }
                else {
                    if (isBossFlag)
                    {
                        //员工备注不可编辑
                        this.richTextBoxEx2.Enabled = false;
                        //上司备注可编辑
                        this.richTextBoxEx1.Enabled = true;
                        //承认
                        this.buttonX2.Image = global::WorkFlowSystem.Properties.Resources.check;
                        this.buttonX2.Text = "承认";
                        //否认
                        this.buttonX1.Image = global::WorkFlowSystem.Properties.Resources.stop;
                        this.buttonX1.Text = "否认";
                    }
                    else
                    {
                        //员工备注可编辑
                        this.richTextBoxEx2.Enabled = true;
                        //上司备注不可编辑
                        this.richTextBoxEx1.Enabled = false;

                    }
                }
                PanelEx pl = new PanelEx();

                //标准PANEL的样式
                string panelType = "";

                //标签名
                string panelName = "";

                //填写数据的DB列名
                string workDataColumn = "";

                //填写的数据
                string workData = "";

                string sValue1 = "";

                //上司备注
                if (dt.Rows[0]["w_manager_notes"] != null)
                {
                    this.richTextBoxEx1.Text = dt.Rows[0]["w_manager_notes"].ToString();
                }

                //员工备注
                if (dt.Rows[0]["w_notes"] != null)
                {
                    this.richTextBoxEx2.Text = dt.Rows[0]["w_notes"].ToString();
                }

                //记录期间
                if (dt.Rows[0]["w_date_name"].ToString() == "D")
                {
                    this.labelX3.Text = dt.Rows[0]["w_start_date"].ToString();
                }
                else
                {
                    this.labelX3.Text = dt.Rows[0]["w_date_name"].ToString();
                }


                for (int i = 0; i < dataCnt; i++)
                {   
                    //标准PANEL的样式
                    panelType = dt.Rows[i]["s_tab_type"].ToString();

                    //标签名
                    panelName = dt.Rows[i]["s_tab_name"].ToString();

                    //填写数据的DB列名
                    workDataColumn = "w_tab_" + dt.Rows[i]["s_tab_no"].ToString();

                    //填写的数据
                    workData = dt.Rows[i][workDataColumn].ToString();

                    //标准第一个值
                    sValue1 = dt.Rows[i]["s_value_1"].ToString();

                    if ("RADIO".Equals(panelType))
                    {
                        WiTemplate wir1 = new WiTemplate();

                        //RADIO标签名
                        wir1.labelX1.Text = panelName;

                        //第一个RADIO的标签名
                        wir1.radioButton1.Text = dt.Rows[i]["s_value_1"].ToString();
                        wir1.radioButton1.Name = "data";
                        rba[0] = wir1.radioButton1;
                                                
                        wir1.radioButton2.Visible = false;
                        wir1.radioButton3.Visible = false;
                        wir1.radioButton4.Visible = false;
                        wir1.radioButton5.Visible = false;

                        if (!"".Equals(dt.Rows[i]["s_value_2"].ToString()))
                        {
                            wir1.radioButton2.Text = dt.Rows[i]["s_value_2"].ToString();
                            wir1.radioButton2.Visible = true;
                            wir1.radioButton2.Name = "data";
                            rba[1] = wir1.radioButton2;                            
                        }
                        if (!"".Equals(dt.Rows[i]["s_value_3"].ToString()))
                        {
                            wir1.radioButton3.Text = dt.Rows[i]["s_value_3"].ToString();
                            wir1.radioButton3.Visible = true;
                            wir1.radioButton3.Name = "data";
                            rba[2] = wir1.radioButton3;
                        }
                        if (!"".Equals(dt.Rows[i]["s_value_4"].ToString()))
                        {
                            wir1.radioButton4.Text = dt.Rows[i]["s_value_4"].ToString();
                            wir1.radioButton4.Visible = true;
                            wir1.radioButton4.Name = "data";
                            rba[3] = wir1.radioButton4;
                        }
                        if (!"".Equals(dt.Rows[i]["s_value_5"].ToString()))
                        {
                            wir1.radioButton5.Text = dt.Rows[i]["s_value_5"].ToString();
                            wir1.radioButton5.Visible = true;
                            wir1.radioButton5.Name = "data";
                            rba[4] = wir1.radioButton5;
                        }
                        
                        rba[int.Parse(workData)].Checked = true;

                        this.panel2.Controls.Add(wir1.panelExRadio);
                    }

                    else if ("PROGRESS".Equals(panelType))
                    {
                        WiTemplate wir2 = new WiTemplate();
                        
                        //标签名
                        wir2.labelX3.Text = panelName;
                        
                        //进度的值
                        if (workData != "")
                        {
                            wir2.slider1.Value = int.Parse(workData);
                        }
                        else
                        {
                            wir2.slider1.Value = 0;
                        }

                        wir2.slider1.Name = "data";
                        
                        this.panel2.Controls.Add(wir2.panelExProgress);
                    }

                    else if ("TARGET".Equals(panelType))
                    {
                        WiTemplate wir3 = new WiTemplate();

                        //标签名
                        wir3.labelX2.Text = panelName;

                        //当天或者一共完成的值
                        wir3.textBoxX1.Text = workData;

                        //如果是日期型记录，则求各个日期的合计
                        if ("D".Equals(dt.Rows[i]["w_date_name"].ToString()))
                        {
                            dbo.ConnectDB();

                            //获取标准合计值
                            string sql_sum = " select sum(" + workDataColumn + ") as data_sum from work_info where " +
                                         " w_start_date >= @w_start_date and w_end_date <=@w_end_date and w_uid = @w_uid and w_sid = @w_sid and w_date_name = 'D' ";


                            List<SqlParameter> para_list_sum = new List<SqlParameter>();

                            para_list_sum.Add(new SqlParameter("@w_sid", this.sid));
                            para_list_sum.Add(new SqlParameter("@w_uid", this.uid));
                            para_list_sum.Add(new SqlParameter("@w_start_date", this.w_start_date));
                            para_list_sum.Add(new SqlParameter("@w_end_date", this.w_start_date));

                            DataTable dtSum = new DataTable();

                            dbo.ExecuteDataTable(sql_sum, para_list_sum, false, ref dtSum);

                            dbo.CloseDB();

                            wir3.labelX9.Text = dtSum.Rows[0]["data_sum"].ToString();


                        }
                        //否则用填写的实际值
                        else
                        {
                            wir3.labelX9.Text = dt.Rows[0][workDataColumn].ToString();
                        }

                        
                        //目标值
                        wir3.labelX7.Text = sValue1;
                        wir3.textBoxX1.Name = "data";

                        //进度条的百分比文字

                        if (workData != "")
                        {
                            wir3.labelX4.Text = (double.Parse(workData) / double.Parse(sValue1) * 100).ToString() + "%";

                            if ((double.Parse(workData) / double.Parse(sValue1) * 100 > 100))
                            {
                                wir3.progressBar1.Value = 100;
                            }
                            else
                            {
                                wir3.progressBar1.Value = (int)(double.Parse(workData) / double.Parse(sValue1) * 100 + 0.5);
                            }
                            
                        }
                        else
                        {
                            wir3.labelX4.Text = "0%";
                            wir3.progressBar1.Value = 0;
                        }


                        this.panel2.Controls.Add(wir3.panelExTarget);
                    }

                    else
                    {
                        MessageBox.Show("数据异常!");
                    }

                }
                return 1;
            }
        }

        //获取当前表单填写值
        private ArrayList GetInputValue()
        {

            ArrayList arrayList = new ArrayList();

            int radioCheckedIndex = 0;

            inputCheck = true;
            
            Control[] c = this.Controls.Find("data", true);

            for (int i = c.Count()-1; i >= 0;i-- )
            {

                switch (c[i].GetType().Name)
                {


                    case "RadioButton":
                        RadioButton rd = (RadioButton)c[i];
                        if (!rd.Checked)
                        {
                            radioCheckedIndex++;

                        }
                        else
                        {
                            arrayList.Add(radioCheckedIndex.ToString());

                        }
                        break;

                    case "TextBoxX":

                        TextBoxX tb = (TextBoxX)c[i];

                        int tmp = 0;

                        if (!"".Equals(tb.Text.ToString()))
                        {
                            tmp = Convert.ToInt32(tb.Text);
                        }
                        else
                        {
                            MessageBox.Show("该项不能为空！");

                            this.inputCheck = false;
                            tb.Focus();

                            break;
                        }

                        arrayList.Add(tmp);


                        break;


                    case "Slider":
                        Slider sld = (Slider)c[i];
                        arrayList.Add(sld.Value.ToString());

                        break;

                    default:
                        break;

                }

            }
            return arrayList;

        }


        //记录更新处理
        public int dataUpdate(string updateFlag)
        {
            if (isBossFlag)
            {
                List<SqlParameter> para_list = new List<SqlParameter>();

                string w_manager_notes = "";

                string updateSql = "UPDATE work_info SET  w_status = @w_status, w_manager_notes = @w_manager_notes, w_update_time = getdate() ,w_update_uid = @w_update_uid ";

                string whereSql = " WHERE w_uid = @w_uid AND w_sid = @w_sid and w_start_date =@w_start_date and w_end_date = @w_end_date ";

                //否认处理
                if (!"admit".Equals(updateFlag))
                {
                    if (richTextBoxEx1.Text.Length == 0)
                    {
                        MessageBox.Show("请输入否认原因。");
                        return 0;
                    }
                    else
                    {
                        w_manager_notes = richTextBoxEx1.Text.ToString();

                        para_list.Add(new SqlParameter("@w_status", "已否认"));
                    }
                }
                //承认处理
                else
                {
                    w_manager_notes = richTextBoxEx1.Text.ToString();
                    para_list.Add(new SqlParameter("@w_status", "已承认"));
                }

                //上司备注
                para_list.Add(new SqlParameter("@w_manager_notes", w_manager_notes));
                //用户ID
                para_list.Add(new SqlParameter("@w_uid", uid));
                //标准
                para_list.Add(new SqlParameter("@w_sid", sid));
                //开始日期
                para_list.Add(new SqlParameter("@w_start_date", w_start_date));
                //结束日期
                para_list.Add(new SqlParameter("@w_end_date", w_end_date));
                //更新者ID
                para_list.Add(new SqlParameter("@w_update_uid", CurrentUser.user_id));

                DBOperator dbo = new DBOperator();
                dbo.ConnectDB();

                if (dbo.ExecuteNonQuery(updateSql + whereSql, para_list, false) > 0)
                {
                    dbo.CloseDB();
                    return 1;
                }
                else
                {
                    dbo.CloseDB();
                    return 0;
                }

            }
            else
            {

                ArrayList al = this.GetInputValue();

                if (inputCheck == false)
                {
                    return 0;
                }
                else
                {
                    DBOperator dbo = new DBOperator();
                    dbo.ConnectDB();

                    string updateSql = "UPDATE work_info SET w_tab_1 = @w_tab_1,w_tab_2 = @w_tab_2,w_tab_3 = @w_tab_3,w_tab_4 = @w_tab_4,w_tab_5 = @w_tab_5,w_tab_6 = @w_tab_6,w_tab_7 = @w_tab_7,w_tab_8 = @w_tab_8,w_tab_9 = @w_tab_9,w_tab_10 = @w_tab_10, w_status = @w_status, w_notes = @w_notes, w_update_time = getdate() ,w_update_uid = @w_update_uid ";

                    string whereSql = " WHERE w_uid = @w_uid AND w_sid = @w_sid and w_start_date =@w_start_date and w_end_date = @w_end_date ";

                    List<SqlParameter> para_list = new List<SqlParameter>();

                    for (int i = 0; i < 10; i++)
                    {

                        if (al.Count > i)
                        {
                            para_list.Add(new SqlParameter("@w_tab_" + (i + 1).ToString(), al[i].ToString()));
                        }
                        else
                        {
                            para_list.Add(new SqlParameter("@w_tab_" + (i + 1).ToString(), "0"));
                        }

                    }

                    //用户ID
                    para_list.Add(new SqlParameter("@w_uid", uid));
                    //标准
                    para_list.Add(new SqlParameter("@w_sid", sid));
                    //开始日期
                    para_list.Add(new SqlParameter("@w_start_date", w_start_date));
                    //结束日期
                    para_list.Add(new SqlParameter("@w_end_date", w_end_date));

                    para_list.Add(new SqlParameter("@w_update_uid", CurrentUser.user_id));

                    if ("save".Equals(updateFlag))
                    {
                        //记录状态
                        para_list.Add(new SqlParameter("@w_status", "已保存"));
                    }
                    else if ("commit".Equals(updateFlag))
                    {
                        //记录状态
                        para_list.Add(new SqlParameter("@w_status", "已提交"));
                    }

                    //如果员工备注不为空
                    if (!"".Equals(this.richTextBoxEx2.Text.ToString()))
                    {
                        //员工备注
                        para_list.Add(new SqlParameter("@w_notes", this.richTextBoxEx2.Text.ToString()));
                    }
                    else
                    {
                        //员工备注
                        para_list.Add(new SqlParameter("@w_notes", ""));
                    }

                    if (dbo.ExecuteNonQuery(updateSql + whereSql, para_list, false) > 0)
                    {
                        dbo.CloseDB();
                        return 1;
                    }
                    else
                    {
                        dbo.CloseDB();
                        return 0;
                    }

                }
            }                    

        }

     
        //保存处理 承认处理
        private void buttonX2_Click(object sender, EventArgs e)
        {
            if (isBossFlag)
            {
                if ("已承认".Equals(w_status))
                {
                    MessageBox.Show("该工作记录已经被承认，不可重复承认。");
                }
                else
                {
                    if (0 == this.dataUpdate("admit"))
                    {
                        MessageBox.Show("处理失败!");
                    }
                    else
                    {
                        MessageBox.Show("处理成功!");
                    }

                }
            }
            else
            {
                if ("已承认".Equals(w_status))
                {
                    MessageBox.Show("该工作记录已经被承认，不可编辑。");

                }
                else
                {
                    if (0 == this.dataUpdate("save"))
                    {
                        MessageBox.Show("保存失败!");
                    }
                    else
                    {
                        MessageBox.Show("保存成功!");
                    }

                }
            }    

        }

        //提交处理 否认处理
        private void buttonX1_Click(object sender, EventArgs e)
        {
            if (isBossFlag)
            {
                if ("已承认".Equals(w_status))
                {
                    MessageBox.Show("该工作记录已经被承认，不可否认。");
                }
                else
                {
                    if (0 == this.dataUpdate("deny"))
                    {
                        MessageBox.Show("处理失败!");
                    }
                    else
                    {
                        MessageBox.Show("处理成功!");
                    }

                }
            }
            else
            {
                if ("已承认".Equals(w_status))
                {
                    MessageBox.Show("该工作记录已经被承认，不可编辑。");

                }
                else
                {

                    if (0 == this.dataUpdate("commit"))
                    {
                        MessageBox.Show("提交失败!");
                    }
                    else
                    {
                        MessageBox.Show("提交成功!");
                    }
                }
            }
            
        }
    }
}
